# encoding=utf-8_general_ci

import tornado.web
import sae.const
import tornado.database
import os.path

from comment import comment
from article import article
from connection import Connection

class PraiseHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
    	self.post()
    @tornado.web.asynchronous
    def post(self):
        query = self.get_argument("query", "")
        if(query == ""):
            return

        ip = self.request.remote_ip
        db = Connection(sae.const.MYSQL_HOST+":"+sae.const.MYSQL_PORT,sae.const.MYSQL_DB, sae.const.MYSQL_USER, sae.const.MYSQL_PASS, 30)

        temp = db.query("SELECT * FROM Praise WHERE id='"+str(query)+"' and ip='"+str(ip) + "'")
        
        if len(temp) == 0:
            db.execute("UPDATE article SET Praise=praise+1 WHERE Id=" + str(query))
            db.execute("INSERT INTO Praise (id, ip) VALUES ('"+str(query)+"','" +str(ip) +"')")
            self.write("1")
        else:
            self.write("0")
        self.finish()
        return